package com.useeinfo.oa.modules.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.DictInfo;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xudong.sun
 * @Date: 2018/12/7 10:46
 * @Description:
 */
@Repository("dictInfoDao")
public class DictInfoDao implements CrudDao<DictInfo> {

    private final static Logger logger = LoggerFactory.getLogger(EnterpriseDao.class);

    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        String typeCode = queryHash.get("typeCode");
        if (!StringUtils.isEmpty(typeCode)) {
            conditionHash.put("(s.typeCode = ?{paramIndex}) ",typeCode );
        }
        String dictInfoId = queryHash.get("dictInfoId");
        if (!StringUtils.isEmpty(dictInfoId)) {
            conditionHash.put("s.id = ?{paramIndex} ", StringConverters.ToLong(dictInfoId));
        }
        String propertyCode = queryHash.get("propertyCode");
        if(!StringUtils.isEmpty(propertyCode)){
            conditionHash.put("s.propertyCode = ?{paramIndex}",propertyCode);
        }
        String propertyValue = queryHash.get("propertyValue");
        if(!StringUtils.isEmpty(propertyValue)){
            conditionHash.put("s.propertyValue = ?{paramIndex}",propertyValue);
        }
        String magicLike = queryHash.get("magicLike");
        if (StringUtils.isNotBlank(magicLike)) {
            conditionHash.put("s.propertyName like ?{paramIndex} ", "%" + magicLike + "%");
        }
        String magicId = queryHash.get("magicId");
        if (StringUtils.isNotBlank(magicId)) {
            conditionHash.put("s.id != ?{paramIndex} ", StringConverters.ToLong(magicId));
        }
        String propertyName = queryHash.get("propertyName");
        if(StringUtils.isNotBlank(propertyName)){
            conditionHash.put("(s.propertyName = ?{paramIndex})",propertyName);
        }
        String propertyNameLike = queryHash.get("propertyNameLike");
        if (StringUtils.isNotBlank(propertyNameLike)) {
            conditionHash.put("( s.propertyName like ?{paramIndex}) ", "%" + propertyNameLike + "%");
        }
        String propertyCodeLike = queryHash.get("propertyCodeLike");
        if (StringUtils.isNotBlank(propertyCodeLike)) {
            conditionHash.put("( s.propertyCode like ?{paramIndex}) ", "%" + propertyCodeLike + "%");
        }
        String typeNameLike = queryHash.get("typeNameLike");
        if (StringUtils.isNotBlank(typeNameLike)) {
            conditionHash.put("( s.typeName like ?{paramIndex}) ", "%" + typeNameLike + "%");
        }
        String typeCodeLike = queryHash.get("typeCodeLike");
        if (StringUtils.isNotBlank(typeCodeLike)) {
            conditionHash.put("( s.typeCode like ?{paramIndex}) ", "%" + typeCodeLike + "%");
        }

        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("s.enterprise.id = ?{paramIndex} ", StringConverters.ToLong(enterpriseId));
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from DictInfo s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<DictInfo> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from DictInfo s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<DictInfo> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, DictInfo.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public DictInfo findModel(Long id) {
        return entityManager.find(DictInfo.class, id);
    }

    @Override
    public Integer add(DictInfo model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("DictInfoDaoImpl添加dictInfo成功！");
        return 1;
    }

    @Override
    public Integer update(DictInfo model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        DictInfo dictInfo = entityManager.find(DictInfo.class, id);
        entityManager.remove(dictInfo);
        return 1;
    }

    @Override
    public Integer deleteLogic(DictInfo model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
